package com.hung.view.page;

import com.hung.dao.MatterDao;
import com.hung.dao.daoImpl.PageDaoImpl;
import com.hung.entity.PageBean;
import com.hung.pojo.Account;
import com.hung.pojo.Matter;
import com.hung.service.serviceImpl.MatterServiceImpl;
import com.hung.util.ListToVector;
import com.hung.util.orm.sqlsession.defaults.DefaultSqlSession;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

/**
 * 瓜圈页面
 *
 * @author Hung
 */
public class PartComponent extends Box {

    /**
     * title表格提示,data表格内容
     */
    private JTable table;
    private Vector<String> title = new Vector<>();
    private Vector<Vector> data = new Vector<>();
    private DefaultTableModel tableModel;

    MatterDao matterDao = (MatterDao) new DefaultSqlSession().getMapper(MatterDao.class);
    JFrame jf = null;

    public PartComponent(int pid, Account account, JFrame jf, JSplitPane jSplitPane) {
        super(BoxLayout.Y_AXIS);
        this.jf = jf;
        //组装视图
        JPanel btnPanel = new JPanel();
        btnPanel.setBackground(Color.white);
        btnPanel.setMaximumSize(new Dimension(850, 80));
        btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        //上方菜单栏
        JButton post = new JButton("来发布吧");
        post.addActionListener(actionEvent -> {
            AddMatterDialog dialog = new AddMatterDialog(jf, "发表你的大瓜", true, account, pid);
            dialog.show();
        });
        JLabel searchLabel = new JLabel("搜索:");
        JTextField fuzzyQueryField = new JTextField(15);
        JButton searchBtn = new JButton("查询");
        searchBtn.addActionListener(actionEvent -> {
            String condition = fuzzyQueryField.getText().trim();
            createTable(null,condition,pid);
            tableModel.fireTableDataChanged();
        });
        btnPanel.add(searchLabel);
        btnPanel.add(fuzzyQueryField);
        btnPanel.add(searchBtn);
        btnPanel.add(post);


        //组装表格
        String[] titles = {"序号", "内容", "作者", "发布时间", "赞"};
        for (String t : titles) {
            title.add(t);
        }

        //模糊查询返回页数
        String condition = fuzzyQueryField.getText().trim();
        int page = new PageDaoImpl().queryTotalCount(condition)/9+1;

        //根据页数创建页数按钮添加到下方
        JPanel pageBtn = new JPanel();

        for (int i = 0; i < page; i++) {
            String currentPage = i + "";
            JButton button = new JButton(i + 1 + "");
            pageBtn.add(button);
            button.addActionListener(actionEvent -> {
                createTable(currentPage, condition, pid);
                //刷新表格
                tableModel.fireTableDataChanged();
            });
        }

        //初始化页面
        createTable(null, condition, pid);
        tableModel = new DefaultTableModel(data, title);

        //设置table
        table = new JTable(tableModel) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //双击获取信息进去新页面
                if (e.getClickCount() == 2) {
                    int selectedRow = table.getSelectedRow();
                    //获取matter信息进入新页面
                    Object value = table.getValueAt(selectedRow, 0);
                    Matter matter = matterDao.queryMatterById(Integer.parseInt(value.toString()));
                    //设置右界面
                    jSplitPane.setRightComponent(new MatterComponent(account, matter, jf, jSplitPane, pid));
                }
            }
        });

        //组装视图
        Box box = Box.createVerticalBox();
        box.add(btnPanel);
        box.add(new JScrollPane(table));
        box.add(pageBtn, BorderLayout.SOUTH);
        this.add(box);
    }

    public void createTable(String currentPage, String condition, int pid) {
        MatterServiceImpl matterService = new MatterServiceImpl();
        //获取相应页面的数据
        PageBean<Matter> dataOnPage = matterService.findDataOnPage(currentPage, "10", condition, pid);
        List<Matter> dataList = dataOnPage.getList();
        Vector<Vector> vectors = ListToVector.mListToVector(dataList);

        //清空数据,添加数据
        data.clear();
        for (Vector vector : vectors) {
            data.add(vector);
        }
    }

}
